package com.fw.controller;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fw.model.Permission;
import com.fw.model.Role;
import com.fw.service.RoleService;
import com.fw.service.impl.RoleServiceImpl;
import com.fw.util.RolePermissonsZtree;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.jfinal.core.Controller;

public class RoleController extends Controller {
	RoleService roleService = null;
	public void toRoleListView() {
		renderJsp("/WEB-INF/jsp/role_list.jsp");
	}
	public void addRole(){
		Role role = getModel(Role.class);
		roleService = new RoleServiceImpl();
		boolean flag = roleService.add_roleService(role);
		String message = null;
		if(flag){
			message = "添加成功";
			setAttr("message",message);
		}
		else{
			message = "添加失败";
			setAttr("message",message);
		}
		renderJsp("/WEB-INF/jsp/cue_message.jsp");
	}
	public void toAddRoleView(){
		renderJsp("/WEB-INF/jsp/add_role.jsp");
	}
	public void query_allRoleByPage() throws UnsupportedEncodingException{
		StringBuffer sql = new StringBuffer();
		HttpServletRequest request = getRequest();
		request.setCharacterEncoding("utf-8");
		HttpServletResponse response = getResponse();
		response.setContentType("text/html;charset = utf-8");
		//显示的起始索引
		int limitStart = Integer.parseInt(getPara("start").toString());
		//显示的行数
		int limitLength = Integer.parseInt(getPara("length").toString());
		String draw = "0";
		draw = getPara("draw");
		roleService = new RoleServiceImpl();
		int totalData = roleService.query_totalRoleService(sql.toString());
		List<Role> rs = roleService.query_allRoleByPageService(sql.toString(), limitStart, limitLength);
		Map<Object, Object> info = new HashMap<Object, Object>();
		info.put("data",rs);
		info.put("recordsTotal", totalData);
		info.put("recordsFiltered", totalData);
		info.put("draw", draw);
		renderJson(info);
	}
	public void delete_role(){
		Role role = getModel(Role.class);
		roleService = new RoleServiceImpl();
		boolean flag = roleService.delete_roleByIdService(role);
	    renderJson(flag);
	}
	public void toUpDateRoleView(){
		Role role = getModel(Role.class);
		roleService = new RoleServiceImpl();
		Role r = roleService.query_roleByIdService(role);
		setAttr("role", r);
		renderJsp("/WEB-INF/jsp/update_role.jsp");
	}
	public void updateRole(){
		Role role = getModel(Role.class);
		roleService = new RoleServiceImpl();
		boolean flag = roleService.update_roleByIdService(role);
		String message = null;
		if(flag){
			message = "编辑成功";
			setAttr("message",message);
		}
		else{
			message = "编辑失败";
			setAttr("message",message);
		}
		renderJsp("/WEB-INF/jsp/cue_message.jsp");
	}
	public void query_rolePermissonsZtreeData(){
		Role role = getModel(Role.class);
		roleService = new RoleServiceImpl();
		List<RolePermissonsZtree> list = roleService.query_rolePermissonsZtreeDataService(role);
		Gson gson = new GsonBuilder().registerTypeAdapter(RolePermissonsZtree.class, new JsonSerializer<RolePermissonsZtree>() {
			@Override
			public JsonElement serialize(RolePermissonsZtree rpz, Type arg1,JsonSerializationContext arg2) {
				JsonObject o = new JsonObject();
				o.addProperty("id", rpz.getPermission().getInt("p_id"));
				o.addProperty("name", rpz.getPermission().getStr("p_name"));
				o.addProperty("pId", rpz.getPermission().getInt("parent_id"));
				o.addProperty("checked", rpz.isChecked());
					return o;
				}
			}).create();
		renderText(gson.toJson(list));
	}
	public void toRolePermissionUpdateView(){
		renderJsp("/WEB-INF/jsp/role_permission_update.jsp");
	}
	public void update_rolePermission(){
		Role role = getModel(Role.class);
		String nids = getPara("nids");
		String[] nidss = nids.split(",");
		List<Permission> permissions = null;
		if(null != nids && "" != nids){
			permissions = new ArrayList<Permission>();
			for(int i = 0;i<nidss.length;i++){
				Permission p = new Permission();
				p.put("p_id",nidss[i]);
				permissions.add(p);
			}
		}
		roleService = new RoleServiceImpl();
		boolean flag = false;
		flag = roleService.update_rolePermissionService(role, permissions);
		renderJson(flag);
	}
}
